{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "G=[\n",
    "    [2,3,4],\n",
    "    [0],\n",
    "    [3,4],\n",
    "    [5],\n",
    "    [1],\n",
    "    [0],   \n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Color(Enum):\n",
    "    White=0\n",
    "    Gray=1\n",
    "    Black=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def dfs_sweep(G):\n",
    "    n=len(G)\n",
    "    color=[Color.White for i in range(n)] # 初始化为white\n",
    "    GT=[ [] for i in range(n)] # 初始化GT GT为G的转置图\n",
    "    for v in range(n): # for v =0 to n-1\n",
    "        if color[v]==Color.White:\n",
    "            vAns=dfs(G,color,v,GT)\n",
    "            # 处理vAns\n",
    "def dfs(G,color,v,GT):\n",
    "    color[v]=Color.Gray\n",
    "    # 处理顶点v preorder processing\n",
    "    ans=0\n",
    "    remAdj=G[v].copy()\n",
    "    while remAdj:\n",
    "        w=remAdj.pop(0)\n",
    "        if color[w]==Color.White:\n",
    "            # 处理边vw tree edge\n",
    "            wAns=dfs(G,color,w,GT)\n",
    "            # 回溯处理边vw nontree edge\n",
    "        else:\n",
    "            # 检查边vw nontree edge\n",
    "    # 处理顶点v postorder processing\n",
    "    color[v]=Color.black\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "G=[\n",
    "    [2,3,4],\n",
    "    [0],\n",
    "    [3,4],\n",
    "    [5],\n",
    "    [1],\n",
    "    [0],   \n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def GraphTranspose(G):\n",
    "    \"\"\" 图的转置 \"\"\"\n",
    "    n=len(G)\n",
    "    color=[Color.White for i in range(n)] # 初始化为white\n",
    "    GT=[ [] for i in range(n)] # 初始化GT GT为G的转置图\n",
    "    for v in range(n): # for v =0 to n-1\n",
    "        if color[v]==Color.White:\n",
    "            dfs(G,color,v,GT)\n",
    "    return GT\n",
    "\n",
    "def dfs(G,color,v,GT):\n",
    "    \"\"\"使用深度优先遍历 \"\"\"\n",
    "    color[v]=Color.Gray\n",
    "    remAdj=G[v].copy() \n",
    "    while remAdj:\n",
    "        w=remAdj.pop(0) \n",
    "        GT[w].append(v) # wv边把添加到GT\n",
    "        if color[w]==Color.White:    \n",
    "            dfs(G,color,w,GT) # 继续遍历\n",
    "    color[v]=Color.Black\n",
    "    return "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[5, 1], [4], [0], [2, 0], [2, 0], [3]]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dfs_sweep(G)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
